package com.ruoyi.web.core.config;

import java.util.Date;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.ruoyi.common.utils.AccessTokenUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.JedisPoolUtil;
import com.ruoyi.common.utils.WeiXinHttpUtil;
import com.ruoyi.common.utils.bean.WeixinAccount;
import com.ruoyi.jiugongge.domain.WebJwid;
import com.ruoyi.jiugongge.service.IWebJwidService;



@Component
public class RefreshTokenTask {
	public final static Logger LOG = LoggerFactory.getLogger(RefreshTokenTask.class);
	@Autowired
	private IWebJwidService myJwWebJwidService;
	
	@Bean
	@Scheduled(fixedRate = 1000*60*90)
	public void run() {
		LOG.info("===================重置AccseeToken定时任务开启==========================");
		LOG.info("现在时间:"+DateUtils.getTime()+"");
		long start = System.currentTimeMillis();
		try {
			Date date = new Date();
			long time= date.getTime()-1000*60*90;
			Date refDate = new Date(time);
			List<WebJwid> myJwWebJwids = myJwWebJwidService.queryResetTokenList(refDate);
			for(WebJwid myJwWebJwid:myJwWebJwids){
				try {
					Map<String, Object> map = AccessTokenUtil.getAccseeToken(myJwWebJwid.getWeixinAppid(), myJwWebJwid.getWeixinAppsecret());
					if(map!=null && map.containsKey("status")){
						if("success".equals(map.get("status"))){
							String accessToken = map.get("accessToken").toString();
							myJwWebJwid.setAccessToken(accessToken);
							myJwWebJwid.setTokenGettime(date);
							//获取api(卡券用)
							myJwWebJwid.setApiticket(map.get("apiTicket").toString());
							myJwWebJwid.setApiticketGettime(date);
							//获取jsapi(JS-SDK用)
							myJwWebJwid.setJsapiticket(map.get("jsApiTicket").toString());
							myJwWebJwid.setJsapiticketGettime(date);
							
							myJwWebJwidService.updateWebJwid(myJwWebJwid);
							//-------H5平台独立公众号，重置redis缓存-------------------------------------------
							try {
								WeixinAccount po = new WeixinAccount();
								po.setAccountappid(myJwWebJwid.getWeixinAppid());
								po.setAccountappsecret(myJwWebJwid.getWeixinAppsecret());
								po.setAccountaccesstoken(myJwWebJwid.getAccessToken());
								po.setAddtoekntime(myJwWebJwid.getTokenGettime());
								po.setAccountnumber(myJwWebJwid.getWeixinNumber());
								po.setApiticket(myJwWebJwid.getApiticket());
								po.setApiticketGettime(myJwWebJwid.getApiticketGettime());
								po.setAccounttype(myJwWebJwid.getAccountType());
								po.setWeixinAccountid(myJwWebJwid.getJwid());//原始ID
								po.setJsapiticket(myJwWebJwid.getJsapiticket());
								po.setJsapitickettime(myJwWebJwid.getJsapiticketGettime());
								JedisPoolUtil.putWxAccount(po);
							} catch (Exception e) {
								LOG.error("----------定时任务：重置redis缓存token失败-------------"+e.toString());
							}
							//--------H5平台独立公众号，重置redis缓存---------------------------------------
						}
					}
				} catch (Exception e) {
					LOG.info("重置AccseeToken定时任务异常e={}"+e.getMessage());
				}
			}
		} catch (Exception e) {
			LOG.info("重置AccseeToken定时任务异常e={}",new Object[]{e});
		}
		LOG.info("===================重置AccseeToken定时任务结束，用时={}ms.==========================",new Object[]{System.currentTimeMillis()-start});
	}
}